<template>
    <div class="login-container">
        <div class="temp">
            <el-row :gutter="10">
                <el-col :span="8">
                </el-col>
                <el-col :span="8">
                    <div class="form-container">
                        <img class="logo" src="../assets/logo1.png" alt="logo">
                        <h1>在线客服聊天系统</h1>
                        <div class="form-box">
                            <el-form :model="ruleForm" :rules="rules" ref="ruleFormRef" class="demo-ruleForm">
                                <el-form-item prop="username">
                                    <el-input placeholder="登录账号" v-model="ruleForm.username">
                                        <template #prefix>
                                            <el-icon class="el-input__icon">
                                                <user/>
                                            </el-icon>
                                        </template>
                                    </el-input>
                                </el-form-item>
                                <el-form-item prop="password">
                                    <el-input placeholder="登录密码" show-password v-model="ruleForm.password">
                                        <template #prefix>
                                            <el-icon class="el-input__icon">
                                                <lock/>
                                            </el-icon>
                                        </template>
                                    </el-input>
                                </el-form-item>
                                <el-form-item class="login-btn-box">
                                    <el-button type="primary" :loading="commonData.btnLoading" @click="handleLogin">登录
                                    </el-button>
                                </el-form-item>
                                <div class="flex space-between">
                                    <router-link to="/register">用户注册</router-link>
                                    <router-link to="/register">忘记密码?</router-link>
                                </div>
                            </el-form>
                        </div>
                    </div>
                </el-col>
                <el-col :span="8">
                </el-col>
            </el-row>
        </div>
    </div>
</template>

<script setup>
    import {User, Lock} from '@element-plus/icons-vue';
    import {ref, reactive, getCurrentInstance} from 'vue';
    import {useRouter} from 'vue-router';
    import {useStore} from 'vuex';
    import {api} from '../utils/api';
    import {service} from '../utils/service';

    const router = useRouter();
    const store = useStore();
    const ruleFormRef = ref();
    const {proxy} = getCurrentInstance();
    const ruleForm = reactive({
        username: 'admin',
        password: 'admin123'
    });
    const rules = reactive({
        username: [
            {required: true, message: '请输入登录账号', trigger: 'blur'}
        ],
        password: [
            {required: true, message: '请输入登录密码', trigger: 'blur'}
        ],
    });
    const commonData = reactive({
        btnLoading: false
    });

    //处理登录
    const handleLogin = () => {
        ruleFormRef.value.validate((valid) => {
            if (valid) {
                commonData.btnLoading = true;
                service.doPost(api['login'], ruleForm)
                    .then(async (response) => {
                        if (response.code === 1) {
                            proxy.$socket.io.emit('login', response.data);
                            await store.dispatch('handleChangeLogin', true);
                            await store.dispatch('handleChangeUserInfo', response.data);
                            await router.push({
                                path: '/index'
                            });
                        }
                        commonData.btnLoading = false;
                    }).catch(() => {
                    commonData.btnLoading = false;
                })
            }
        })
    };
</script>

<style lang="less" scoped>

    .login-container {
        display: flex;
        align-items: center;
        height: 100%;

        overflow: hidden;
        background: url("../assets/login-bg1.jpg") no-repeat center;
        -webkit-background-size: cover;
        background-size: cover;

        .temp {
            flex: 1;
        }

        .form-container {
            background: #fff;
            box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
            padding: 15px;
            -webkit-border-radius: 10px;
            -moz-border-radius: 10px;
            border-radius: 10px;

            input {
                font-size: 12px !important;
            }

            .logo {
                width: 50px;
            }

            h1 {
                font-size: 20px;
                margin: 0 auto;
            }

            .form-box {
                max-width: 250px;
                margin: 30px auto;

                .login-btn-box {
                    margin-bottom: 10px;
                }

                button {
                    display: block;
                    width: 100%;
                    margin-top: 10px;
                }
            }
        }
    }
</style>